package net.georel.representation.categorisation.analysis;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;


import org.geotools.feature.FeatureIterator;
import org.opengis.feature.simple.SimpleFeature;

import rcaller.RCaller;
import rcaller.RCode;

public class R_Classifications {
	private static RCaller caller = new RCaller();
	
	private static void initRscriptExe(){
		caller.setRscriptExecutable("C:/Program Files/R/R-2.13.0/bin/Rscript");
		caller.cleanRCode();
	}
	
	public static int[] getKMeansClassification (double[] relevanceArray, int numClasses) throws IOException{
		initRscriptExe();
	    
	    RCode code = new RCode();
	    code.R_require("classInt");
	    code.addDoubleArray("x", relevanceArray);   
	    code.addRCode("result <- classIntervals(x, n="+numClasses+", style=\"kmeans\")");
	    code.addRCode("ci <- findCols(result)");
	    caller.setRCode(code);
	    caller.runAndReturnResult("ci");

	    int[] resultValues = caller.getParser().getAsIntArray("ci");
	    
	    return resultValues;
	    
	    
	    
	    
	}
	
	public static int[] getQuantileClassification (double[] relevanceArray, int numClasses) throws IOException{
		initRscriptExe();
	    
	    RCode code = new RCode();
	    code.R_require("classInt");
	    code.addDoubleArray("x", relevanceArray);   
	    code.addRCode("result <- classIntervals(x, n="+numClasses+", style=\"quantile\")");
	    code.addRCode("ci <- findCols(result)");
	    caller.setRCode(code);
	    caller.runAndReturnResult("ci");
	    
	    int[] resultValues = caller.getParser().getAsIntArray("ci");
	    return resultValues;
	    
	    
	    
	}
	
	public static int[] getJenksClassification (double[] relevanceArray, int numClasses) throws IOException{
		initRscriptExe();
	    
	    RCode code = new RCode();
	    code.R_require("classInt");
	    code.addDoubleArray("x", relevanceArray);   
	    code.addRCode("result <- classIntervals(x, n="+numClasses+", style=\"jenks\")");
	    code.addRCode("ci <- findCols(result)");
	    caller.setRCode(code);
	    caller.runAndReturnResult("ci");
	    
	    int[] resultValues = caller.getParser().getAsIntArray("ci");
	    return resultValues;
	    
	    
	}
	
	public static int[] getSDClassification (double[] relevanceArray, int numClasses) throws IOException{
		initRscriptExe();
	    
	    RCode code = new RCode();
	    code.R_require("classInt");
	    code.addDoubleArray("x", relevanceArray);   
	    code.addRCode("result <- classIntervals(x, n="+numClasses+", style=\"sd\")");
	    code.addRCode("ci <- findCols(result)");
	    caller.setRCode(code);
	    caller.runAndReturnResult("ci");
	    
	    int[] resultValues = caller.getParser().getAsIntArray("ci");
	    return resultValues;
	    
	    
	}
	
	public static int[] getFisherClassification (double[] relevanceArray, int numClasses) throws IOException{
		initRscriptExe();
	    
	    RCode code = new RCode();
	    code.R_require("classInt");
	    code.addDoubleArray("x", relevanceArray);   
	    code.addRCode("result <- classIntervals(x, n="+numClasses+", style=\"fisher\")");
	    code.addRCode("ci <- findCols(result)");
	    caller.setRCode(code);
	    caller.runAndReturnResult("ci");

	    int[] resultValues = caller.getParser().getAsIntArray("ci");
	    return resultValues;
	    
	    
	}
	
	public static int[] getBaggedClusterClassification (double[] relevanceArray, int numClasses) throws IOException{
		initRscriptExe();
	    
	    RCode code = new RCode();
	    code.R_require("classInt");
	    code.addDoubleArray("x", relevanceArray);   
	    code.addRCode("result <- classIntervals(x, n="+numClasses+", style=\"bclust\")");
	    code.addRCode("ci <- findCols(result)");
	    caller.setRCode(code);
	    caller.runAndReturnResult("ci");

	    int[] resultValues = caller.getParser().getAsIntArray("ci");
	    return resultValues;
	    
	    
	}
	
	public static int[] getHierarchicalClusterClassification (double[] relevanceArray, int numClasses) throws IOException{
		initRscriptExe();
	    
	    RCode code = new RCode();
	    code.R_require("classInt");
	    code.addDoubleArray("x", relevanceArray);   
	    code.addRCode("result <- classIntervals(x, n="+numClasses+", style=\"bclust\")");
	    code.addRCode("ci <- findCols(result)");
	    caller.setRCode(code);
	    caller.runAndReturnResult("ci");

	    int[] resultValues = caller.getParser().getAsIntArray("ci");
	    return resultValues;
	    
	    
	}
	
	
}
